<template>
  <div class="login-container">
    <el-row>
      <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
        <img src="../../assets/indexBack.png" alt="" />
        <div
          style="
            position: absolute;
            top: 30%;
            left: 25%;
            transform: translate(-50%, -30%);
            color: #fff;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
          "
        >
          <p
            style="
              font-weight: 300;
              font-size: 48px;
              margin: 0;
              margin-bottom: 30px;
            "
          >
            WMS
          </p>
          <div style="width: 80px; height: 1px; background-color: #fff"></div>
          <p style="font-size: 24px; margin: 0; margin-top: 30px">
            仓库管理系统
          </p>
        </div>
      </el-col>
      <el-col
        :xs="12"
        :sm="12"
        :md="12"
        :lg="12"
        :xl="12"
        style="height: 100vh"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          class="login-form"
          label-position="left"
        >
          <div class="form-back">
            <h2 style="margin: 0.5rem 0">
              <span style="text-decoration: underline #1950ad">欢迎</span>
              登录
            </h2>
            <!-- <div class="lock-back"><img src="@/assets/lock.png" alt="" /></div> -->
            <el-form-item style="margin-top: 0.2rem" prop="username">
              <!-- <span class="svg-container svg-container-admin">
                <img src="@/assets/account.png" alt="" />
              </span> -->
              <el-input
                v-model.trim="form.username"
                v-focus
                placeholder="请输入用户名"
                tabindex="1"
                type="text"
              />
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                :key="passwordType"
                ref="password"
                v-model.trim="form.password"
                :type="passwordType"
                tabindex="2"
                placeholder="请输入密码"
                @keyup.enter.native="handleLogin"
              />
              <span
                v-if="passwordType === 'password'"
                class="show-password"
                @click="handlePassword"
              >
                <vab-icon :icon="['fas', 'eye-slash']"></vab-icon>
              </span>
              <span v-else class="show-password" @click="handlePassword">
                <vab-icon :icon="['fas', 'eye']"></vab-icon>
              </span>
            </el-form-item>
            <el-button
              :loading="loading"
              class="login-btn"
              @click="handleLogin"
            >
              登录
            </el-button>
          </div>
          <!-- <router-link to="/register">
            <div style="margin-top: 20px">注册</div>
          </router-link> -->
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { isPassword } from '@/utils/validate'

  export default {
    name: 'Login',
    directives: {
      focus: {
        inserted(el) {
          el.querySelector('input').focus()
        },
      },
    },
    data() {
      const validateusername = (rule, value, callback) => {
        if ('' == value) {
          callback(new Error('用户名不能为空'))
        } else {
          callback()
        }
      }
      const validatePassword = (rule, value, callback) => {
        if (!isPassword(value)) {
          callback(new Error('密码不能少于6位'))
        } else {
          callback()
        }
      }
      return {
        nodeEnv: process.env.NODE_ENV,
        title: this.$baseTitle,
        form: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            {
              required: true,
              trigger: 'blur',
              validator: validateusername,
            },
          ],
          password: [
            {
              required: true,
              trigger: 'blur',
              validator: validatePassword,
            },
          ],
        },
        loading: false,
        passwordType: 'password',
        redirect: undefined,
      }
    },
    watch: {
      $route: {
        handler(route) {
          this.redirect = (route.query && route.query.redirect) || '/'
        },
        immediate: true,
      },
    },
    created() {
      document.body.style.overflow = 'hidden'
    },
    beforeDestroy() {
      document.body.style.overflow = 'auto'
    },
    mounted() {
      this.form.username = ''
      this.form.password = ''
    },
    methods: {
      handlePassword() {
        this.passwordType === 'password'
          ? (this.passwordType = '')
          : (this.passwordType = 'password')
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      handleLogin() {
        this.loading = true
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.loading = true

            this.$store
              .dispatch('user/login', this.form)
              .then(() => {
                const routerPath =
                  this.redirect === '/404' || this.redirect === '/401'
                    ? '/'
                    : this.redirect
                this.$router.push('/index').catch(() => {})
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            return false
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .login-container {
    height: 100vh;
    // background: url('~@/assets/login_images/background.png') center center fixed
    //   no-repeat;
    // background-size: cover;
    background-color: #fff;
    .title {
      font-size: 0.54rem;
      font-weight: 500;
      color: rgba(14, 18, 26, 1);
    }

    .project_jc {
      font-size: 0.64rem;
      color: #fff;
      margin: 0 0.1rem;
    }

    .title-tips {
      margin: 0.1rem;
      font-size: 0.32rem;
      font-weight: 400;
      color: #fff;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .form-back {
      width: 35%;
      border: 1px solid #efefef;
      padding: 0rem 0.2rem 0.8rem;
      box-sizing: border-box;
      box-shadow: 0rem 0rem 0.1rem 0.03rem #f0f0f0;
    }

    .lock-back {
      padding: 0.17rem;
      background: linear-gradient(transparent, rgba(255, 255, 255, 0.5));
      width: fit-content;
      border-radius: 50%;
      position: absolute;
      width: 0.4rem;
      top: 30%;
      left: 50%;
      transform: translate(-50%, 0%);
    }

    .login-btn {
      display: inherit;
      width: 100%;
      height: 0.4rem;
      margin-top: 0.05rem;
      border: 0;
      border-radius: 0.4rem;
      &:hover {
        opacity: 0.9;
      }
    }

    ::v-deep {
      .el-form-item {
        width: 70%;
        margin: 0.18rem auto !important;
      }

      .el-button {
        width: 70%;
        background-color: #1950ad !important;
        color: #fff !important;
        font-size: 0.14rem;
        font-weight: bold;
        letter-spacing: 0;
        margin: 0.3rem auto 0;
      }

      .el-input input {
        padding-left: 0.6rem !important;
        background: transparent !important;
        border: 0.02rem solid #fff !important;
        color: #fff !important;
      }
    }

    .login-form {
      max-width: 100%;
      margin: auto;
      overflow: hidden;
      // width: 25%;
      text-align: center;
      display: flex;
      height: 100%;
      align-items: center;
      justify-content: center;

      .forget-password {
        width: 100%;
        text-align: left;

        .forget-pass {
          width: 0.129rem;
          height: 0.19rem;
          font-size: 0.2rem;
          font-weight: 400;
          color: rgba(92, 102, 240, 1);
        }
      }
    }

    .tips {
      margin-bottom: 0.1rem;
      font-size: $base-font-size-default;
      color: $base-color-white;

      span {
        &:first-of-type {
          margin-right: 0.16rem;
        }
      }
    }

    .title-container {
      position: relative;

      .title {
        margin: 0 auto 0.4rem auto;
        font-size: 0.34rem;
        font-weight: bold;
        color: $base-color-blue;
        text-align: center;
      }
    }

    .svg-container {
      position: absolute;
      top: 50%;
      left: 0.15rem;
      z-index: $base-z-index;
      font-size: 0.16rem;
      color: #d7dee3;
      cursor: pointer;
      user-select: none;
      transform: translate(0rem, -40%);
    }

    .show-password {
      position: absolute;
      top: 0.1rem;
      right: 0.25rem;
      font-size: 0.16rem;
      color: #d7dee3;
      cursor: pointer;
      user-select: none;
    }

    ::v-deep {
      .el-form-item {
        padding-right: 0;
        margin: 0.2rem 0;
        color: #454545;
        background: transparent;
        border: 0.01rem solid transparent;
        border-radius: 0.02rem;

        &__content {
          min-height: $base-input-height;
          line-height: $base-input-height;
        }

        &__error {
          position: absolute;
          top: 100%;
          left: 0.18rem;
          font-size: $base-font-size-small;
          line-height: 0.18rem;
          color: $base-color-red;
        }
      }

      .el-input {
        box-sizing: border-box;
        border: 2px solid #efefef;
        border-radius: 0.4rem;
        color: #000;
        input {
          height: 0.4rem;
          padding-left: 0.2rem !important;
          font-size: $base-font-size-default;
          line-height: 0.4rem;
          border-radius: 50%;
          color: #000;
          color: $base-font-color;
          background: #f6f4fc;
          caret-color: $base-font-color;
        }
      }

      .el-input--small .el-input__inner {
        line-height: 0.4rem !important;
        height: 0.4rem !important;
        color: #666 !important;
        font-size: 0.14rem !important;
      }
    }
    img {
      width: 100%;
      height: 100vh;
    }
  }
</style>
